var fs = require("fs");
var _ = require("lodash");

let fileNameB = "../extra";
let fileNameC = "./data/character3";
let fileNameD = "./paramDes";
let outputFileName = "./lua/character.lua";

let extraData = JSON.parse(fs.readFileSync(`${fileNameB}.json`).toString()).character;
let paramData = JSON.parse(fs.readFileSync(`${fileNameD}.json`).toString()).map;
let mainData = JSON.parse(fs.readFileSync(`${fileNameC}.json`).toString());

let extraMap = new Map();
_.forEach(extraData, (e) => {
    let id = e.id;
    extraMap.set(id, e);
});
let paramMap = new Map();
_.forEach(paramData, (e) => {
    let id = e.id;
    paramMap.set(id, e.data);
});

function addItemToMap(map, item, count, i) {
    if (!map[item]) map[item] = {};
    map[item][i] = count
}

function getUncap(uncap) {
    if (!uncap) return "''"
    let str = "{\n"
    let itemMap = {};
    let i = 1;
    _.forEach(uncap, (e) => {
        _.forEach(e.uncapItems, (x) => {
            addItemToMap(itemMap, x.itemId, x.count, i)
        })
        i++;
    })
    let j = i - 1;
    for(itemId in itemMap) {
        str += `        [${itemId}] = {`
        for(let i = 1;i < j;i++) {
            str += `${i == 1 ? '' : ','}${itemMap[itemId][i] || 0}`
        }
        str += "},\n";
    }
    str += "    }";
    return str;
}

function getSpGet(uncap) {
    if (!uncap) return "''"
    let str = "{\n"
    let itemMap = {};
    let i = 1;
    _.forEach(uncap, (e) => {
        _.forEach(e.items, (x) => {
            addItemToMap(itemMap, x.itemId, x.count, i)
        })
        i++;
    })
    let j = i;
    for(itemId in itemMap) {
        str += `        [${itemId}] = {`
        for(let i = 1;i < j;i++) {
            str += `${i == 1 ? '' : ','}${itemMap[itemId][i] || 0}`
        }
        str += "},\n";
    }
    str += "    }";
    return str;
}

function getKochab(uncap) {
    if (!uncap) return "''"
    let str = "{\n"
    let itemMap = {};
    let i = 1;
    _.forEach(uncap, (e) => {
        _.forEach(e.kochabFormationItems, (x) => {
            addItemToMap(itemMap, x.itemId, x.count, i)
        })
        i++;
    })
    let j = i - 1;
    for(itemId in itemMap) {
        str += `        [${itemId}] = {`
        for(let i = 1;i < j;i++) {
            str += `${i == 1 ? '' : ','}${itemMap[itemId][i] || 0}`
        }
        str += "},\n";
    }
    str += "    }";
    return str;
}

function getSpUncap(uncap) {
    if (!uncap) return "''"
    let str = "{\n"
    let itemMap = {};
    let i = 1;
    _.forEach(uncap, (e) => {
        if (e.uncapGold) {
            addItemToMap(itemMap, 701308, e.spUncapGold, i)
        }
        _.forEach(e.spUncapItems, (x) => {
            addItemToMap(itemMap, x.itemId, x.count, i)
        })
        i++;
    })
    let j = i - 1;
    for(itemId in itemMap) {
        str += `        [${itemId}] = {`
        for(let i = 1;i < j;i++) {
            str += `${i == 1 ? '' : ','}${itemMap[itemId][i] || 0}`
        }
        str += "},\n";
    }
    str += "    }";
    return str;
}

function getParam(type, section, element) {
    if (!paramMap.has(type)) { 
        console.log("null", type, section, element)
        return null;
    }
    section = section || 0;
    element = element || 0;
    let data = paramMap.get(type);
    if (type != 3) return {
        name: data.name, 
        icon: data.icon
    };
    for (x of data.detail){
        if (_.includes(x.section, section) && _.includes(x.element, element)) {
            return {
                name: x.name, 
                icon: x.icon
            }
        }
    }
    return null
}

function getZx(rankUp) {
    if (!rankUp || !rankUp.showBonus) return "''"
    let d = rankUp.showBonus.param;
    let b1 = getParam(d[0].type, d[0].section, d[0].element) || {}
    let b2 = getParam(d[1].type, d[1].section, d[1].element) || {}
    let p = rankUp.showExBonus.param;
    let p1 = getParam(p[0].type, p[0].section, p[0].element) || {}
    return `{
        ['b1'] = {
            ['name'] = '${b1.name}',
            ['value'] = ${parseFloat(d[0].value[4].toFixed(2))},
            ['icon'] = ${b1.icon},
        },
        ['b2'] = {
            ['name'] = '${b2.name}',
            ['value'] = ${parseFloat(d[1].value[4].toFixed(2))},
            ['icon'] = ${b2.icon},
        },
        ['p'] = {
            ['name'] = '${p1.name}',
            ['value'] = ${parseFloat(p[0].value[0].toFixed(2))},
            ['icon'] = ${p1.icon},
        }
    }`;
}

let output = `local p = {}
`

_.forEach(mainData, (e) => {
    let data = e.data;
    let exData = extraMap.has(e.id) ? extraMap.get(e.id) : {};
    if (!exData) {
        console.log("noextra:", e.id)
    }
    let str = `p['${e.id}'] = {
    ['name'] = '${data.name}',
    ['element'] = '${data.characterElement}',
    ['minAtk'] = '${data.characterMinAtk}',
    ['minHp'] = '${data.characterMinHp}',
    ['maxAtk'] = '${data.characterMinAtk + 59 * data.characterAddAtk}',
    ['maxHp'] = '${data.characterMinHp + 59 * data.characterAddHp}',
    ['race'] = '${data.race}',
    ['rarity'] = '${data.characterRarity}',
    ['type'] = '${data.characterType_}',
    ['weapon1'] = '${data.characterWeaponType1}',
    ['weapon2'] = '${data.characterWeaponType2 || ''}',
    ['age'] = '${data.age}',
    ['height'] = '${data.height}',
    ['birth'] = '${data.birth}',
    ['gender'] = '${data.gender}',
    ['favourite'] = '${data.favourite}',
    ['jpVoice'] = '${data.jpVoice}',
    ['designer'] = '${data.designer}',
    ['prefix'] = '${data.prefix}',
    ['enName'] = '${data.enName}',
    ['addDate'] = ${exData.add_date || "''"},
    ['uncap'] = ${getUncap(data.characterUncap)},
    ['kochab'] = ${getKochab(data.kochabRankUp)},
    ['spuncap'] = ${getSpUncap(data.spChaPasUncap)},
    ['spget'] = ${getSpGet(exData.sp_get)},
    ['zx'] = ${getZx(data.rankUp[0])},
    ['zx_img'] = '${exData.img || ''}',
    ['zx_b1'] = '${exData.zx.b_1 || ''}',
    ['zx_b2'] = '${exData.zx.b_2 || ''}',
    ['zx_p'] = '${exData.zx.p || ''}',
    ['td_atk'] = '${data.gachaBounsAtk.length > 0 ? data.gachaBounsAtk[data.gachaBounsAtk.length - 1] * 60 : ''}',
    ['td_hp'] = '${data.gachaBounsHp.length > 0 ? data.gachaBounsHp[data.gachaBounsHp.length - 1] * 60 : ''}',
    ['nickname'] = '${exData.nickname ? exData.nickname: ''}',
}
`;
    output += str;
});
output += `return p`

fs.writeFileSync(outputFileName, output);
